<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章类别管理
        </div>
        <div class="container-fluid common_con">
            <table class="table table-striped table-bordered table-hover mp20 category_table">
                <thead>
                    <tr>
                        <th>名称</th>
                        <th>Slug</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>

                </tbody>
                <tfoot>
                    <tr>
                        <td colspan="3" class="text-center">
                            <button class="btn btn-success" id="xinzengfenlei" data-toggle="modal"
                                data-target="#myModal">新增分类</button>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
    <!-- bootstrap提供的模态框 -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel"></h4>
                </div>
                <div class="modal-body">
                    <form>
                        <!-- input标签中有一种标签天生就是隐藏的: 隐藏域,不想给用户看到,但是程序员需要使用 -->
                        <input id="category_id" value="" type="hidden" name="id">
                        <div class="form-group">
                            <label for="recipient-name" class="control-label">分类名称:</label>
                            <input type="text" class="form-control" id="recipient-name" name="name">
                        </div>
                        <div class="form-group">
                            <label for="message-text" class="control-label">分类别名:</label>
                            <input type="text" class="form-control" id="message-text" name="slug"></input>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="addOrEdit">Send message</button>
                </div>
            </div>
        </div>
    </div>
</body>
<script src="./libs/https.js"></script>
<script>
    $(function () {
        // 发起ajax请求获取数据
        function getInfo() {
            $.ajax({
                url: BigNew.category_list,
                type: 'GET',
                success: (res) => {
                    if (res.code == 200) {
                        // 利用模板字符串生成结构
                        let htmlStr = ''
                        $.each(res.data, function (index, value) {
                            htmlStr += `
                    <tr>
                        <td>${value.name}</td>
                        <td>${value.slug}</td>
                        <td class="text-center">
                            <a href="javascript:void(0)" data-id=${value.id} data-target="#myModal" data-toggle="modal" class=" btn btn-info btn-xs">编辑</a>
                            <a href="javascript:void(0)" data-id=${value.id} id='delete' class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                    `
                        });
                        $('tbody').html(htmlStr);
                    }
                }
            })
        }
        getInfo();
        // 判断模态框是由哪个按钮触发的
        $('#myModal').on('show.bs.modal', function (e) {
            if (e.relatedTarget.innerText == '新增分类') {
                $('#myModalLabel').text('新增分类');
                $('#addOrEdit').text('新增');
            } else {
                $('#myModalLabel').text('编辑分类');
                $('#addOrEdit').text('编辑');

                // 根据id发起请求
                let dataId = $(e.relatedTarget).attr('data-id');
                $.ajax({
                    url: BigNew.category_search,
                    data: {
                        id: dataId
                    },
                    success: (res) => {
                        if (res.code == 200) {
                            // 渲染模态框
                            $('#category_id').val(res.data[0].id);
                            $('#recipient-name').val(res.data[0].name);
                            $('#message-text').val(res.data[0].slug);
                        }
                    }
                })
            }
        })

        // 完成文章新增,编辑功能
        $('#addOrEdit').on('click', function () {
            if ($(this).text() == '新增') {
                // 新增功能
                let name = $('#recipient-name').val().trim();
                let slug = $('#message-text').val().trim();
                // 判断非空
                if (name == '' && slug == '') {
                    alert('请输入内容');
                    return;
                }
                // 发起新增请求
                $.ajax({
                    url: BigNew.category_add,
                    data: {
                        name,
                        slug,
                    },
                    type: 'POST',
                    success: (res) => {
                        if (res.code == 201) {
                            // 影藏模态框,重新渲染页面
                            $('#myModal').modal('hide');
                            getInfo();
                        }
                    }
                })
            } else {
                // 编辑功能
                let data = $('form').serialize();
                // 获取表单数据,发起请求
                $.ajax({
                    type: 'POST',
                    url: BigNew.category_edit,
                    data,
                    success: (res) => {
                        if (res.code == 200) {
                            alert('修改数据成功');
                            $('#myModal').modal('hide');
                            getInfo();
                        }
                    }
                })
            }
        })

        //4.删除按钮 ：这个按钮是动态添加的，所以需要注册委托事件
        //注意点：注册委托事件的父元素不能是动态添加的，否则无法委托
        $('tbody').on('click', '#delete', function () {
            let dataId = $(this).attr('data-id');
            if (confirm('你确定要删除吗')) {
                $.ajax({
                    url: BigNew.category_delete,
                    type: 'post',
                    data: {
                        id: dataId
                    },
                    success: function (res) {
                        if (res.code == 204) {
                            getInfo();
                        }
                    }
                });
            }
        });
    })
</script>

</html>